package com.dcsgo.myTankGameServers.common;

import java.io.Serializable;
import java.util.Vector;

/**
 * @author DCSGO
 * @version 1.0
 */

@SuppressWarnings({"all"})
public class EnemyTank extends Tank implements Runnable, Serializable {
    public Vector<Shot> shots = new Vector<>();
    public Vector<EnemyTank> otherTanks = new Vector<>();
    public boolean isLive = true;

    public void shotPlayer() {
        Shot shot = new Shot(getX(), getY(), getDirection(), getSpeed() + 3);
        new Thread(shot).start();
        shots.add(shot);
    }

    public boolean beHit(Shot shot) {
        if (shot != null) {
            //坦克方向为上下
            if (getDirection() == 0 || getDirection() == 2) {
                //被击中
                return shot.x >= getX() && shot.y >= getY()
                        && shot.x <= getX() + 40 && shot.y <= getY() + 60;
            } else {//坦克方向为左右
                return shot.x >= getX() && shot.y >= getY()
                        && shot.x <= getX() + 60 && shot.y <= getY() + 40;
            }
        }
        return false;
    }

    //其他坦克在当前坦克的路径上才不能移动
    @Override
    public void moveUp() {
        //遍历其他坦克的位置判断 当前坦克是否可以移动
        for (int i = 0; i < otherTanks.size(); i++) {
            EnemyTank other = otherTanks.get(i);
            if (other.isLive) {
                //其他坦克的方向为上下
                if (other.getDirection() == 0 || other.getDirection() == 2) {
                    if (((getX() >= other.getX() && getX() <= other.getX() + 40)
                            || (getX() + 40 >= other.getX() && getX() + 40 <= other.getX() + 40))
                            && getY() - getSpeed() <= other.getY() + 60 && getY() > other.getY()) {
                        return;
                    }
                } else {//其他坦克的方向为左右
                    if (((getX() >= other.getX() && getX() <= other.getX() + 60)
                            || (getX() + 40 >= other.getX() && getX() + 40 <= other.getX() + 60))
                            && getY() - getSpeed() <= other.getY() + 40 && getY() > other.getY()) {
                        return;
                    }
                }
            }
        }
        super.moveUp();
    }

    @Override
    public void moveRight() {
        for (int i = 0; i < otherTanks.size(); i++) {
            EnemyTank other = otherTanks.get(i);
            if (other.isLive) {
                //其他坦克的方向为上下
                if (other.getDirection() == 0 || other.getDirection() == 2) {
                    if (((getY() >= other.getY() && getY() <= other.getY() + 60)
                            || (getY() + 40 >= other.getY() && getY() + 40 <= other.getY() + 60))
                            && getX() + 60 + getSpeed() >= other.getX() && getX() < other.getX()) {
                        return;
                    }
                } else {//其他坦克的方向为左右
                    if (((getY() >= other.getY() && getY() <= other.getY() + 40)
                            || (getY() + 40 >= other.getY() && getY() + 40 <= other.getY() + 40))
                            && getX() + 60 + getSpeed() >= other.getX() && getX() < other.getX()) {
                        return;
                    }
                }
            }
        }
        super.moveRight();

    }

    @Override
    public void moveDown() {
        for (int i = 0; i < otherTanks.size(); i++) {
            EnemyTank other = otherTanks.get(i);
            if (other.isLive) {
                //其他坦克的方向为上下
                if (other.getDirection() == 0 || other.getDirection() == 2) {
                    if (((getX() >= other.getX() && getX() <= other.getX() + 40)
                            || (getX() + 40 >= other.getX() && getX() + 40 <= other.getX() + 40))
                            && getY() + getSpeed() + 60 >= other.getY() && getY() < other.getY()) {
                        return;
                    }
                } else {//其他坦克的方向为左右
                    if (((getX() >= other.getX() && getX() <= other.getX() + 60)
                            || (getX() + 40 >= other.getX() && getX() + 40 <= other.getX() + 60))
                            && getY() + getSpeed() + 60 >= other.getY() && getY() < other.getY()) {
                        return;
                    }
                }
            }
        }
        super.moveDown();

    }

    @Override
    public void moveLeft() {
        for (int i = 0; i < otherTanks.size(); i++) {
            EnemyTank other = otherTanks.get(i);
            if (other.isLive) {
                //其他坦克的方向为上下
                if (other.getDirection() == 0 || other.getDirection() == 2) {
                    if (((getY() >= other.getY() && getY() <= other.getY() + 60)
                            || (getY() + 40 >= other.getY() && getY() + 40 <= other.getY() + 60))
                            && getX() - getSpeed() <= other.getX() + 40 && getX() > other.getX()) {
                        return;
                    }
                } else {//其他坦克的方向为左右
                    if (((getY() >= other.getY() && getY() <= other.getY() + 40)
                            || (getY() + 40 >= other.getY() && getY() + 40 <= other.getY() + 40))
                            && getX() - getSpeed() <= other.getX() + 60 && getX() > other.getX()) {
                        return;
                    }
                }
            }
        }
        super.moveLeft();
    }

    @Override
    public void run() {
        //4个方向的移动 开炮
        while (isLive) {
            int operate = (int) (Math.random() * 5);
            switch (operate) {
                case 0://向上移动
                    for (int i = 0; i < 10; i++) {
                        //setDirection(0);
                        moveUp();
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1://向右移动
                    for (int i = 0; i < 10; i++) {
                        //setDirection(1);
                        moveRight();
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2://向下移动
                    for (int i = 0; i < 10; i++) {
                        //setDirection(2);
                        moveDown();
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3://向左移动
                    for (int i = 0; i < 10; i++) {
                        //setDirection(3);
                        moveLeft();
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 4://开炮
                    shotPlayer();
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
            }
            //敌方坦克操作间隔(越短敌人越敏捷)
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public EnemyTank(int x, int y, int speed) {
        super(x, y, speed);
    }
}
